package org.example.struct.array;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author liyishan
 * @date 2024/2/18 10:07
 * @apiNote
 */

public class LCR181ReverseMessage {

    public static void main(String[] args) {
        String message = "a good   example";
        System.out.println(reverseMessage(message));
    }

    public static String reverseMessage(String message) {
        String[] arr = message.split(" ");
        StringBuilder sb = new StringBuilder();
        for (int i = arr.length - 1; i >= 0; i--) {
            if (!Objects.equals(arr[i], "")) {
                sb.append(arr[i]).append(" ");
            }
        }
        return sb.toString().trim();
    }

    public static String reverseMessage1(String message) {
        message = message.trim();
        List<String> words = Arrays.asList(message.split("\\s+"));
        Collections.reverse(words);
        return String.join(" ", words);
    }
}
